package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.bean.OrderDetail;
import com.itheima.reggie.bean.Orders;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersMapper mapper;

    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    public IPage<OrdersDto> userPage(Integer page, Integer pageSize) {

        Page<Orders> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,1477573805667852289L);
        mapper.selectPage(page1,null);

        Page<OrdersDto> dtoPage = new Page<>();
        List<OrdersDto> list = page1.getRecords().stream().map(orders -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            Long ordersId = orders.getId();

            List<OrderDetail> byOrder = orderDetailService.findByOrderId(ordersId);
            ordersDto.setOrderDetails(byOrder);

            return ordersDto;

                }
        ).collect(Collectors.toList());

        dtoPage.setRecords(list);


        return dtoPage;
    }
}
